<template>
  <div class="head-search">
    <el-form ref="searchFormRef" :inline="true" :label-width="labelWidth" :size="size" :model="modelValue"
      @submit.native.prevent>
      <el-row>
        <slot></slot>
        <template v-if="showMore">
          <slot name="more"></slot>
        </template>
        <el-col :span="8">
          <el-form-item class="search-button">
            <el-button style="margin-right: 10px;" link @click="closeSearch" v-if="moreSearch">
              {{ showMore ? '收起' : '展开' }}
              <el-icon style="color: #000;" v-if="showMore">
                <el-icon-arrow-up />
              </el-icon>
              <el-icon style="color: #000;" v-else>
                <el-icon-arrow-down />
              </el-icon>
            </el-button>
            <el-button type="primary" icon="el-icon-search" :size="size" @click="search">查询</el-button>
            <el-button type="info" plain icon="el-icon-delete" :size="size"
              @click="resetForm(searchFormRef)">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script lang="ts">
export default { name: 'AutumnSearch' }
</script>

<script lang="ts" setup>
import { ref } from 'vue'
import type { FormInstance } from 'element-plus'

const searchFormRef = ref<FormInstance>()

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => { }
  },
  size: {
    type: String,
    default: () => 'default'
  },
  labelWidth: {
    type: [String, Number],
    default() {
      return '100px'
    }
  },
  moreSearch: {
    type: Boolean,
    default: () => false
  }
})

const emits = defineEmits(['search', 'reset'])

// 搜索/重置
const search = function () {
  emits('search')
}
const resetForm = function (formEl: FormInstance | undefined) {
  if (!formEl) return
  formEl.resetFields()
  emits('reset')
  search()
}

//展开更多
const showMore = ref(false)
const closeSearch = function () {
  showMore.value = !showMore.value
}

</script>

<style lang="scss" scoped>
.search-button {
  button:nth-child(1) ~ button {
    margin-left: 5px;
  }
}
</style>
